﻿#region Copyright 
/*-------------------------------------------------------------------------
* 命名空间名称/文件名:    SSPivot.Common/SSPivotException 
* CLR版本:     4.0.30319.42000
* 机器名称:    DESKTOP-NHMLO8A
* 功 能：       N/A
* 类 名：       SSPivotException
* 创建时间：  2025/5/21 18:35:31
* 版本号：      v1.0
* 创建人:        xulong
*-------------------------------------------------------------------------*/
#endregion
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Text;
using System.Threading.Tasks;

namespace SSPivot.Common
{ 
    /// <summary>
    /// 自定义异常接口
    /// </summary>
    public interface ISSPivotException
    {

    }
     /// <summary>
     /// 业务逻辑异常
     /// </summary>
    public class SSPivotException : System.Exception, ISSPivotException
    {
        #region 属性

        /// <summary>
        /// 友好错误信息（供前端用户使用）
        /// </summary>
        public string FriendlyErrorMsg { get; set; }

        /// <summary>
        /// 错误代码
        /// </summary>
        public string? ErrorCode { get; set; }
        /// <summary>
        /// 状态码
        /// </summary>
        public int StatusCode { get; set; } = (int)HttpStatusCode.OK;

        #endregion
        #region 构造函数
        /// <summary>
        /// 单参数异常
        /// </summary>
        /// <param name="message"></param>
        public SSPivotException(string message)
      : base(message)
        {
            FriendlyErrorMsg = message;
        }
        /// <summary>
        /// 多参数
        /// </summary>
        /// <param name="message"></param>
        /// <param name="code"></param>
        public SSPivotException(string message, string code)
    : base(message)
        {
            FriendlyErrorMsg = message;
            ErrorCode = code;
        }

        /// <summary>
        /// 多参数
        /// </summary>
        /// <param name="message"></param>
        /// <param name="code"></param>
        /// <param name="statusCode"></param>
        public SSPivotException(string message, string code, int statusCode)
   : base(message)
        {
            FriendlyErrorMsg = message;
            ErrorCode = code;
            StatusCode = statusCode;
        }

        /// <summary>
        /// 多参数
        /// </summary>
        /// <param name="message"></param>
        /// <param name="innerException"></param>
        public SSPivotException(string message, System.Exception innerException)
            : base(message, innerException)
        {
            FriendlyErrorMsg = message;
        }
        /// <summary>
        /// 多参数
        /// </summary>
        /// <param name="message"></param>
        /// <param name="code"></param>
        /// <param name="innerException"></param>
        public SSPivotException(string message, string code, System.Exception innerException)
            : base(message, innerException)
        {
            FriendlyErrorMsg = message;
            ErrorCode = code;
        }
        /// <summary>
        /// 多参数
        /// </summary>
        /// <param name="message"></param>
        /// <param name="code"></param>
        /// <param name="statusCode"></param>
        /// <param name="innerException"></param>
        public SSPivotException(string message, string code, int statusCode, System.Exception innerException)
            : base(message, innerException)
        {
            FriendlyErrorMsg = message;
            ErrorCode = code;
            StatusCode = statusCode;
        }
        #endregion

        #region 方法
        /// <summary>
        /// 创建异常
        /// </summary>
        /// <param name="errorCode"></param>
        /// <param name="errorMsg"></param>
        /// <returns></returns>
        public static SSPivotException Create(string errorCode, string errorMsg)
        {
            return new SSPivotException(errorMsg)
            {
                ErrorCode = errorCode,
                FriendlyErrorMsg = errorMsg
            };
        }
        /// <summary>
        ///  创建异常
        /// </summary>
        /// <param name="errorCode"></param>
        /// <param name="errorMsg"></param>
        /// <returns></returns>
        public static SSPivotException Create(HttpStatusCode errorCode, string errorMsg)
        {
            return new SSPivotException(errorMsg)
            {
                ErrorCode = errorCode.ToString(),
                StatusCode = (int)errorCode,
                FriendlyErrorMsg = errorMsg
            };
        }

        /// <summary>
        /// 创建异常
        /// </summary>
        /// <param name="errorMsg"></param>
        /// <returns></returns>
        public static SSPivotException Create(string errorMsg)
        {
            return SSPivotException.Create("-1", errorMsg);
        }
        /// <summary>
        ///  创建异常
        /// </summary>
        /// <param name="errorCode"></param>
        /// <param name="errorMsg"></param>
        /// <returns></returns>
        public static SSPivotException Create(string code, HttpStatusCode statusCode, string errorMsg)
        {
            return new SSPivotException(errorMsg)
            {
                ErrorCode = code,
                StatusCode = (int)statusCode,
                FriendlyErrorMsg = errorMsg
            };
        }
        #endregion
    }
}
